package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "菜品管理接口")
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.saveWithFlavors(dishDTO);

        // 清除与新增菜品id相关联的分类下的菜品数据
        String pattern = "dish_" + dishDTO.getCategoryId();
        cleanCache(pattern);

        return Result.success();
    }

    /**
     * 与其他两个分页查询近乎相同的写法 区别点在于service与mapper
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteBatch(@RequestParam List<Long> ids) { // ids是以逗号分隔的参数 可通过@RequestParam解析
        dishService.deleteBatch(ids);
        // 删除菜品较多 为方便清除全部缓存即可
        cleanCache("dish_*");

        return Result.success();
    }
    @ApiOperation("根据id查询菜品和关联的口味数据")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        // 修改菜品后甚至可以更改菜品的分类信息 受到影响的分类可能较多 因此删除全部缓存即可
        cleanCache("dish_*");
        return Result.success();
    }

    @ApiOperation("菜品启售禁售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable int status,Long id) { // 由此看来后端可同时接收前端传来的路径参数和query参数
        dishService.startOrStop(status,id);
        // 起售禁售业务中并未传递该菜品的分类id 方便期间清理全部缓存
        cleanCache("dish_*");
        return Result.success();
    }

    @ApiOperation("根据分类类型查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId) {
        List<Dish> dishes = dishService.getByCategoryId(categoryId);
        return Result.success(dishes);
    }

    // 清理redis缓存 根据传递的pattern删除关联的keys
    private void cleanCache(String keyPattern) {
        Set keys = redisTemplate.keys(keyPattern);
        redisTemplate.delete(keys);
    }
}
